package znbkSameQuesTranslate.SQ_TKT;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import com.example.znbk.znbklibrary.base.AnswerViewPagerAdapter;
import com.example.znbk.znbklibrary.base.BaseFragment;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import java.util.ArrayList;
import java.util.List;

import znbkSameQuesTranslate.GetSameQuesJson.SameQuesPaperEntity;

@SuppressLint("ValidFragment")
public class SQxlTktFragment extends BaseFragment {

    private String QuesBody = "";
    private String SubChildAsk = "";
    private void getEntityInfor(){
        QuesBody = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getQuesBody();

        String GenreName = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getGenreInfo().getGenreName();
        if(GenreName.equals("选词填空")) {
            int ChildListSize = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().size();
            for (int i = 0; i < ChildListSize; i++) {
                String SubChildAsk_X = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().
                        getChildList().get(i).getSubChildList().get(0).getSubChildAsk()+"<br><br>";
                SubChildAsk = SubChildAsk + SubChildAsk_X;
            }
        }else{
            SubChildAsk = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                    get(0).getSubChildList().get(0).getSubChildAsk()+"<br><br>";
        }

    }

    private List<SameQuesPaperEntity> sameQuesPaperEntity;
    private int quesIndex = -1;
    private CallBackFragmentTkt callBackFragmentTkt;
    public SQxlTktFragment(List<SameQuesPaperEntity> sameQuesPaperEntity, final int quesIndex, CallBackFragmentTkt callBackFragmentTkt){
        this.sameQuesPaperEntity = sameQuesPaperEntity;
        this.quesIndex = quesIndex;
        this.callBackFragmentTkt = callBackFragmentTkt;
        getEntityInfor();
    }

    @Override
    protected int createLayout() {
        return R.layout.sq_fragment_tkt;
    }

    private WebView mWvQuesBody;
    private ViewPager mVpAnswer;
    private ArrayList<View> list;
    public AnswerViewPagerAdapter mAnswerViewPagerAdapter;

    private RelativeLayout mRlControlView;
    private ScaleButton mSBtnSubmit;

    @Override
    protected void initView(View view) {

        //显示Ask题干
        mWvQuesBody = view.findViewById(R.id.wv_quesBody);
        mWvQuesBody.setBackgroundColor(0); // 设置背景色
        mWvQuesBody.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        String WvQuesBody = QuesBody+SubChildAsk;
        mWvQuesBody.loadData(WvQuesBody, "text/html", "utf-8");

        //显示Answer信息
        mVpAnswer = view.findViewById(R.id.vp_answerTkt);
        list = new ArrayList<>();
        list.clear();
        RecyclerView mRvAnswer = new RecyclerView(getActivity());
        mRvAnswer.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager
        SQxlTktAnswerAdapter mAnswerAdapter = new SQxlTktAnswerAdapter(getActivity(), sameQuesPaperEntity, quesIndex);
        mAnswerAdapter.setHasStableIds(true);
        mRvAnswer.setAdapter(mAnswerAdapter);
        int ItemSize  = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getTotalItemCount();
        mRvAnswer.setItemViewCacheSize(ItemSize);
        list.add(mRvAnswer);
        mAnswerViewPagerAdapter = new AnswerViewPagerAdapter(list);
        mVpAnswer.setAdapter(mAnswerViewPagerAdapter);
        sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().setCurrentSubChildAnswerIndex(0);
        mVpAnswer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().setCurrentSubChildAnswerIndex(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //显示提交按钮
        mRlControlView = view.findViewById(R.id.rl_controlView);
        mSBtnSubmit  = view.findViewById(R.id.sbtn_submit);
        mSBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sameQuesPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(2);
                hideSBtnSubmit();
            }
        });

    }

    private void doSubmitRefresh(){
        list.clear();
        RecyclerView mRvAnswer = new RecyclerView(getActivity());
        mRvAnswer.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager
        SQxlTktAnswerAdapter mAnswerAdapter = new SQxlTktAnswerAdapter(getActivity(), sameQuesPaperEntity, quesIndex);
        mAnswerAdapter.setHasStableIds(true);
        mRvAnswer.setAdapter(mAnswerAdapter);
        int ItemSize  = sameQuesPaperEntity.get(quesIndex).getQuesFromTK().getTotalItemCount();
        mRvAnswer.setItemViewCacheSize(ItemSize);
        list.add(mRvAnswer);
        mAnswerViewPagerAdapter = new AnswerViewPagerAdapter(list);
        mVpAnswer.setAdapter(mAnswerViewPagerAdapter);

        callBackFragmentTkt.submitKgtQuesAnswer();
    }

//    private void showSBtnSubmit(){
//        if(mSBtnSubmit.getVisibility()==View.GONE) {
//            mRlSubmitBg.setVisibility(View.VISIBLE);
//            mSBtnSubmit.setVisibility(View.VISIBLE);
//            ObjectAnimator translationY1 = ObjectAnimator.ofFloat(mRlSubmitBg, View.TRANSLATION_Y, 300f, 0f);
//            ObjectAnimator translationY2 = ObjectAnimator.ofFloat(mSBtnSubmit, View.TRANSLATION_Y, 300f, 0f);
//            AnimatorSet setShow = new AnimatorSet();
//            setShow.playTogether(translationY1,translationY2);
//            setShow.setDuration(800);
//            setShow.start();
//        }
//    }

    private void hideSBtnSubmit(){
        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(mRlControlView, View.TRANSLATION_Y, 0f, 300f);
        ObjectAnimator translationY2 = ObjectAnimator.ofFloat(mSBtnSubmit, View.TRANSLATION_Y, 0f, 300f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(translationY1,translationY2);
        setShow.setDuration(600);
        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mRlControlView.setVisibility(View.GONE);
                mSBtnSubmit.setVisibility(View.GONE);
                doSubmitRefresh();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        setShow.start();
    }


    public interface CallBackFragmentTkt {
        void submitKgtQuesAnswer();
    }


}